package com.platform.server.util;

import java.util.Random;

/**
 * <p>
 * 随机数工具类
 * </p>
 * 
 * 
 */
public class RandomUtil {
	private static ThreadLocal<Random> random = new ThreadLocal<Random>() {
		@Override
		protected Random initialValue() {
			return new Random();
		}
	};
	private static final String charBuffer = "0123456789abcdefghijklmnopqrstuvwxyz";
	private static final String charBuffer1 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+{}|:<>?";
	private static final String numberBuffer = "0123456789";

	/**
	 * 获取指定长度随机字符
	 * 
	 * @param length
	 * @return
	 */
	public static String getRandomString(int length) {
		if (length <= 0) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		Random r = random.get();
		int range = charBuffer.length();
		for (int i = 0; i < length; i++) {
			sb.append(charBuffer.charAt(r.nextInt(range)));
		}
		return sb.toString();
	}
	//强密码生成
	public static String getRandomStrings(int length) {
		if (length <= 0) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		Random r = random.get();
		int range = charBuffer1.length();
		for (int i = 0; i < length; i++) {
			sb.append(charBuffer1.charAt(r.nextInt(range)));
		}
		return sb.toString();
	}
public static void main(String[] args) {
	System.out.println(getRandomStrings(30));
}
	/**
	 * 获取指定长度随机数字
	 * 
	 * @param length
	 * @return
	 */
	public static String getRandomNumberString(int length) {
		if (length <= 0) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		Random r = random.get();
		int range = numberBuffer.length();
		for (int i = 0; i < length; i++) {
			sb.append(numberBuffer.charAt(r.nextInt(range)));
		}
		return sb.toString();
	}

	/**
	 * 获取指定长度随机数字，并保证不以0打头
	 * 
	 * @param length
	 * @return
	 */
	public static String getRandomNumberNozeroStart(int length) {
		if (length <= 0) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		Random r = random.get();
		int range = numberBuffer.length();
		sb.append(numberBuffer.charAt(r.nextInt(range - 1) + 1));
		for (int i = 1; i < length; i++) {
			sb.append(numberBuffer.charAt(r.nextInt(range)));
		}
		return sb.toString();
	}
}
